package com.bhald.CJ.lock;

import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

import com.bhald.CJ.Common.Common;
import com.bhald.CJ.Common.CommonM;
import com.bhald.CJ.MethodView.EquipmentLayout;
import com.bhald.CJ.MethodView.LockLayout;
import com.bhald.CJ.MethodView.SetLayout;

public class MainActivity extends Activity {
	private static final String Tag = "MainActivity";

	private String tab_test1 = "tab_test1";
	private String tab_test2 = "tab_test2";
	private String tab_test3 = "tab_test3";
	private String tab_test4 = "tab_test4";
	// query

	// Linear
	private LinearLayout LOCKLinear, EQUIPMENTLinear, APPLIANCESLinear,
			SETLinear, main_linear;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		//
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		//
		setContentView(R.layout.activity_main);

		//
		addwidget();
	}

	@SuppressLint("InlinedApi")
	public void addwidget() {
		//
		// new Thread(new UpdateDriverThread(MainActivity.this)).start();
		//
		LOCKLinear = (LinearLayout) findViewById(R.id.tab1);
		EQUIPMENTLinear = (LinearLayout) findViewById(R.id.tab2);
		APPLIANCESLinear = (LinearLayout) findViewById(R.id.tab3);
		SETLinear = (LinearLayout) findViewById(R.id.tab4);

		LOCKLinear.setTag(Tag);
		main_linear = (LinearLayout) findViewById(R.id.main_linear);
		main_linear.setTag(Tag);
		// //////////////////////////////////////////////////////////////
		// add TabHost
		TabHost mTabHost = (TabHost) findViewById(R.id.tabHost);
		mTabHost.setTag(Tag);
		mTabHost.setup();
		TabWidget tabWidge = mTabHost.getTabWidget();
		mTabHost.addTab(mTabHost.newTabSpec(tab_test1).setIndicator("门锁").setContent(R.id.tab1));
		mTabHost.addTab(mTabHost.newTabSpec(tab_test2).setIndicator("照明").setContent(R.id.tab2));
		mTabHost.addTab(mTabHost.newTabSpec(tab_test3).setIndicator("电器").setContent(R.id.tab3));
		mTabHost.addTab(mTabHost.newTabSpec(tab_test4).setIndicator("设置").setContent(R.id.tab4));

		for (int i = 0; i < tabWidge.getChildCount(); i++) {
			tabWidge.getChildAt(i).getLayoutParams().height = LayoutParams.MATCH_PARENT;
			tabWidge.getChildAt(i).getLayoutParams().width = LayoutParams.MATCH_PARENT;
			TextView tv = (TextView) tabWidge.getChildAt(i).findViewById(android.R.id.title);
			tv.setTextSize(18);
			tv.setTextColor(Color.WHITE);
			tabWidge.getChildAt(i).setBackgroundResource(R.drawable.tab_bg);
		}
		//
		mTabHost.setCurrentTab(0);
		//
		mTabHost.setOnTabChangedListener(new OnTabChangeListener() {
			@Override
			public void onTabChanged(String tabId) {
				if (tabId.equals(tab_test1)) {
					LockLayout myLock = new LockLayout(MainActivity.this,LOCKLinear);
					myLock.Start();
					
				} else if (tabId.equals(tab_test2)) {
					EquipmentLayout myEquipment = new EquipmentLayout(MainActivity.this, EQUIPMENTLinear);
					myEquipment.Start();
				} else if (tabId.equals(tab_test3)) {

				} else if (tabId.equals(tab_test4)) {
					SetLayout mySet = new SetLayout(MainActivity.this,SETLinear);
					mySet.Start();
				}
			}
		});
		//
		CreateLock();

		LockLayout myLock = new LockLayout(MainActivity.this, LOCKLinear);
		myLock.Start();
	}

	// create file from SD

	public void CreateLock() {
		String LockFile = Environment.getExternalStorageDirectory().toString()
				+ "/" + Common.FILENAME;
		if (CommonM.CheckSD()) {
			CommonM.CreateLockFile(LockFile);
		}

		// BufferStore<LockEntity> store = new BufferStore<LockEntity>(LockFile
		// + "/aaa.bhald");
		//
		// List<LockEntity> list = new ArrayList<LockEntity>();
		// for (int i = 0; i < 50; i++) {
		// LockEntity Lock = new LockEntity();
		// Lock.setLockName("aaa" + i);
		// list.add(Lock);
		// }
		// store.write(list, 100);
		//
		// list = null;
		// list = store.read();
		// for (int i = 0; i < list.size(); i++) {
		// Log.e("l", "list" + i + ":" + list.get(i).getLockName());
		// }
	}

	PopupWindow popupWindow;
	boolean boo = true;

	// back
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			exitBy2Click();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	// exit
	private static Boolean isExit = false;

	private void exitBy2Click() {
		Timer tExit = null;
		if (isExit == false) {
			isExit = true;
			Toast.makeText(this, "再按一次退出程序。", Toast.LENGTH_SHORT).show();
			tExit = new Timer();
			tExit.schedule(new TimerTask() {
				@Override
				public void run() {
					isExit = false;
				}
			}, 2000);

		} else {
			finish();
			System.exit(0);
		}
	}

}
